<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 */
namespace app\api\controller;

use Mscore\Core\Controller;
use app\api\model\Login as LoginModel;
use app\api\model\Account as AccountModel;
use app\api\model\Common as CommonModel;
use Mscore\Core\Lang;
use Mscore\Core\Util\Util;

class AccountController extends Controller{

    private $model;
    private $login;
    private $error;
    private $success;
  
    public function __construct()  
    {    
        $this->login = (new LoginModel)->isAuth();
        $this->model = new AccountModel;
        $this->header =get_all_headers();
        $this->header['language']=isset($this->header['language'])?$this->header['language']:config('langs.default');
        Lang::init($this->header['language']);
        $this->success=Lang::get('Success');
        $this->error=Lang::get('Error');
    } 

    /**
     * 获取账户信息
     * @param array $accointinfo 
     * @param array $where 
    */
    public function accointinfo(){
        $this->success['data']=$this->login;
        return $this->echojson($this->success);
    }

    /**
     * 获取用户信息
     * @param array $userinfo 
     * @param array $where 
    */
    public function userinfo(){
        $info = $this->model->info('aid='.$this->login['aid'],['aid','uid','username','paypwd','amount','giftamount','integra','cash','realname','mobile','email','header_url','pid','usercode']);
        $cert = $this->model->cert('aid='.$this->login['aid'],['status']);
        if(isset($cert['status'])){ $info['auth']=$cert['status']; }else{ $info['auth']=1; }
        unset($cert);
        if(!empty($info['header_url'])){
            $info['header_url']=config('file.baseurl').$info['header_url'];
        }
        if(!empty($info['paypwd'])){
            $info['paypwd']=true;
        }else{
            $info['paypwd']=false;
        }
        $this->success['data']=$info;
        return $this->echojson($this->success);
    }

    /**
     * 获取认证及银行账户信息
     * @param array $usercert 
     * @param array $where 
    */
    public function usercert(){
        $info = [];
        $info = $this->model->cert('aid='.$this->login['aid'],['uid','aid','status','certificate','cert_url','notes']);
        $user = $this->model->info('aid='.$this->login['aid'],['paypwd','amount','integra','username']);
        if(!empty($user['paypwd'])){
            $this->success['paypwd']=true;
        }else{
            $this->success['paypwd']=false;
        }
        if(!empty($info)){
            $info['username']=$user['username'];
            $info['amount']=$user['amount'];
            $info['integra']=$user['integra'];
            $info['certificate']=isset($info['certificate'])&&isJson($info['certificate'])?json_decode($info['certificate'],true):[];
            // $info['bankaccount']=isset($info['bankaccount'])&&isJson($info['bankaccount'])?json_decode($info['bankaccount'],true):[];
            $info['cert_url']=isset($info['cert_url'])&&isJson($info['cert_url'])?json_decode($info['cert_url'],true):[];
            if(!empty($info['cert_url'])){
               $info['cert_url']['IDcardF']=isset($info['cert_url']['IDcardF'])&&!empty($info['cert_url']['IDcardF'])?config('file.baseurl').$info['cert_url']['IDcardF']:'';
               $info['cert_url']['IDcardB']=isset($info['cert_url']['IDcardB'])&&!empty($info['cert_url']['IDcardB'])?config('file.baseurl').$info['cert_url']['IDcardB']:'';
            }
        }
        unset($user);
        $this->success['data']=isset($info)&&!empty($info)?$info:['status'=>1];
        return $this->echojson($this->success);
    }

    /**
     * 完善用户信息
     * @param array $updateuser
     * @param array $where 
    */
    public function updateuser(){
        $params = post();
        $params['aid']=$this->login['aid'];
        if(empty($params['realname'])) $this->echoJson(Lang::get('Realnameempty'));
        if(empty($params['mobile'])) $this->echoJson(Lang::get('Mobileempty'));
        $info = $this->model->updateuser($params);
        switch ($info) {
            case true:
                return $this->echojson($this->success);
                break;
            default:
                return $this->echojson(Lang::get('Parerror'));
                break;
        }
    }

    /**
     * 认证信息
     * @param array $getcerttype
     * @param array $where 
    */
    public function gettype(){
        $params = [
            ['name'=>Lang::get('IDcard'),'value'=>1],
            ['name'=>Lang::get('Passport'),'value'=>2],
            ['name'=>Lang::get('Driver'),'value'=>3],
            ['name'=>Lang::get('SSCard'),'value'=>4],
            ['name'=>Lang::get('Other'),'value'=>5],
        ];
        $this->success['data']=$params;
        return $this->echojson($this->success);
    }

    /**
     * 完善认证信息
     * @param array $updatecert
     * @param array $where 
    */
    public function updatecert(){
        $params = post();
        if(empty($params['IDcard'])) $this->echoJson(Lang::get('Idcardempty'));
        if(empty($params['name'])) $this->echoJson(Lang::get('Realnameempty'));
        if(empty($params['IDcardF'])) $this->echoJson(Lang::get('Cardurlempty'));
        if(empty($params['IDcardB'])) $this->echoJson(Lang::get('Cardurlempty'));
        $params['aid']=$this->login['aid'];
        $params['pid']=$this->login['pid'];
        $this->model->updatename(['aid'=>$params['aid'],'realname'=>$params['name']]);
        $info = $this->model->cert('dels<1 and aid='.$params['aid'],['status']);
        $info = isset($info['status'])&&!empty($info['status'])?$info['status']:1;
        switch ($info) {
            case 2:
                return $this->echojson(Lang::get('Realname'));
                break;
            case 3:
                return $this->echojson(Lang::get('Realauth'));
                break;
            default:
                $this->model->updatecert($params);
                return $this->echojson(Lang::get('Success'));
                break;
        }
    }

    /**
     * 修改密码
     * @param array $updatepwd 
     * @param array $where 
    */
    public function updatepwd(){
        $params = post();
        if(empty($params['oldpassword'])) $this->echoJson(Lang::get('Oldpasswordempty'));
        if(empty($params['newpassword'])) $this->echoJson(Lang::get('Passwordempty'));
        $editpwd=(new CommonModel())->getBaseinfo('keyname="edituser"','content');
        $editpwd=isset($editpwd['content']['pwdshow'])&&!empty($editpwd['content']['pwdshow'])?$editpwd['content']['pwdshow']:1;
        if($editpwd==1){
        	if(empty($params['repassword'])) $this->echoJson(Lang::get('Confirmempty'));
            if($params['newpassword'] != $params['repassword']) $this->echoJson(Lang::get('Duplicate'));
        }
        //验证原密码
        $password = $this->model->info('aid='.$this->login['aid'],'password');
        if (!Util::passwordVerify($params['oldpassword'], $password['password'])) {
            $this->error = Lang::get('Pwderror');
            return $this->echojson($this->error);
        }
        $params['aid']=$this->login['aid'];
        unset($params['oldpassword'],$params['repassword'],$password);
        $this->model->updatepwd($params);
        return $this->echojson($this->success);
    }

    /**
     * 修改支付密码
     * @param array $updatepaypwd
     * @param array $where 
    */
    public function updatepaypwd(){
        $params = post();
        if(empty($params['password']) || strlen($params['password']) != 6) $this->echoJson(Lang::get('Paypwdempty'));
        if(empty($params['checkPass'])) $this->echoJson(Lang::get('Duplicate'));
        switch ($params['type']) {
            case 1:
                $paypwd=$this->model->info('aid='.$this->login['aid'],'paypwd');
                if(isset($paypwd['paypwd'])&&!empty($paypwd['paypwd'])){
                    if(empty($params['oldpassword'])) $this->echoJson(Lang::get('Oldpasswordempty'));
                    if (!Util::passwordVerify($params['oldpassword'], $paypwd['paypwd'])) {
                        $this->error = Lang::get('Pwderror');
                        return $this->echojson($this->error);
                    }
                }
                break;
            case 2:
                if(empty($params['code'])) $this->echoJson(Lang::get('Codeempty'));
                //验证驗證码
                $st = (new CommonModel)->getcode($params['code']);
                if (!isset($st) || $st<1) {
                    $this->error = Lang::get('Codeerror');
                    return $this->echojson($this->error);
                }
                break;
            default:
                // return $this->echojson($this->error);
                break;
        }
        $params['aid'] = $this->login['aid'];
        $info = $this->model->updatepaypwd($params);
        switch ($info) {
            case true:
                return $this->echojson($this->success);
                break;
            default:
                return $this->echojson(Lang::get('Parerror'));
                break;
        }
    }

    /**
     * 添加银行
     * @param array $updatebank 
     * @param array $where 
    */
    public function updatebank(){
        $params = post();
        if(empty($params['bank_account'])) $this->echoJson(Lang::get('Banknum'));
        if(empty($params['deposit_branch'])) $this->echoJson(Lang::get('Banknotes'));
        if(empty($params['currency'])) $this->echoJson(Lang::get('Currerror'));
        if($params['type']==1){
            if(empty($params['bank_name'])) $this->echoJson(Lang::get('Bankacc'));
            if(empty($params['deposit_name'])) $this->echoJson(Lang::get('Banknotes'));
        }else{
            $params['bank_name']=$params['currency'];
            $params['deposit_name']=$params['currency'];
        }
        $params['aid']=$this->login['aid'];
        $control=(new CommonModel())->getBaseinfo('keyname="edituser"','content');
        $params['bankaccsum']=isset($control['content']['bankaccsum'])?$control['content']['bankaccsum']:1;
        $params['status']=isset($control['content']['bankstatus'])?$control['content']['bankstatus']:1;
        $info=$this->model->updatebank($params);
        switch (true) {
            case $info:
                return $this->echojson($this->success);
                break;
            default:
                return $this->echojson(Lang::get('Bankerror'));
                break;
        }
        
    }

    /**
     * 银行列表
     * @param array $bank 
     * @param array $where 
    */
    public function bank(){
        $info = $this->model->cert('aid='.$this->login['aid'],['aid','bankaccount']);
        if(isset($info['bankaccount'])&&!empty($info['bankaccount'])){
            $info=json_decode($info['bankaccount'],true);
            foreach ($info as $key => $value) {
                if(isset($value['status'])&&$value['status']!=2){
                    unset($info[$key]);
                }
            }
        }else{
            $info = []; 
        }
        $this->success['data']=$info;
        return $this->echojson($this->success);  
    }

    /**
     * 银行列表
     * @param array $bank 
     * @param array $where 
    */
    public function banklist(){
        $info = $this->model->cert('aid='.$this->login['aid'],['aid','bankaccount']);
        if(isset($info['bankaccount'])&&!empty($info['bankaccount'])){
            $info=json_decode($info['bankaccount'],true);
        }else{
            $info = []; 
        }
        $this->success['data']=$info;
        return $this->echojson($this->success);  
    }

    /**
     * 支持的幣種列表
     * @param array $currency 
     * @param array $where 
    */
    public function currency(){
        $info = $this->model->currency('dels<1 and status>0 and type='.post('type'),['name','codes']);
        $default = (new CommonModel())->getBaseinfo('keyname="payment"','content')['content'];
        if(isset($default['currency'])){ $default=$default['currency']; }else{ $default=''; }
        $this->success['data']=$info;
        $this->success['default']=$default;
        return $this->echojson($this->success);  
    }

    /**
     * 获取支付密碼
     * @param array $getpaypwd 
     * @param array $where 
    */
    public function getpaypwd(){
        $info = $this->model->info('aid='.$this->login['aid'],['paypwd']);
        $info['paypwd']=isset($info['paypwd'])&&!empty($info['paypwd'])?$info['paypwd']:'';
        $this->success['data']=$info;
        return $this->echojson($this->success);
    }

    /**
     * 获取用户修改方式
     * @param array $getedituser 
     * @param array $where 
    */
    public function getedituser(){
        $method = post('method');
        $operinfo=(new CommonModel())->getBaseinfo('keyname="edituser"','content');
        $operinfo=!empty($method)?$operinfo['content'][$method]:$operinfo['content'];
        $this->success['data']=$operinfo;
        return $this->echojson($this->success); 
    }

    /**
     * 获取前端显示菜单
     * @param array $getpermits 
     * @param array $where 
    */
    public function getpermits(){
        $permits=(new CommonModel())->getBaseinfo('keyname="custpermits"','content');
        $permits=isset($permits['content'])&&!empty($permits['content'])?$permits['content']:[];
        $this->success['data']=$permits;
        return $this->echojson($this->success); 
    }

    /**
     * 获取用户修改方式
     * @param array $getbaseinfo 
     * @param array $where 
    */
    public function getbaseinfo(){
        $key = post('key');
        $baseinfo=(new CommonModel())->getBaseinfo('keyname="'.$key.'"','content');
        $baseinfo=$baseinfo['content'];
        $this->success['data']=$baseinfo;
        return $this->echojson($this->success); 
    }

    /**
     * 获取用户修改方式
     * @param array $userlist 
     * @param array $where 
    */
    public function userlist(){
        $where='dels<1 and status>0 and grade="member"';
        $page = 1;
        $limit = 10;
        $filed = 'username,header_url,amount';
        $user=$this->model->userlist($where,$page,$limit,$filed);
        foreach ($user['data'] as $key => $value) {
            if(!empty($value['header_url'])){
                $user['data'][$key]['header_url']=config('file.baseurl').$value['header_url'];
            }
        }
        $this->success['data']=$user['data'];
        $this->success['count']=$user['total'];
        return $this->echojson($this->success); 
    }
    
}